//
//  MyEntranceTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/30.
//

import UIKit

class MyEntranceTableViewCell: UITableViewCell {

    var bgView: UIView!
    var bgLayer1: CALayer!
    var collectionView: UICollectionView!
  
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        self.initUI()
        
    }
    
    func initUI(){
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        // fillCode
        bgLayer1 = CALayer()
        bgLayer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        bgView.layer.addSublayer(bgLayer1)
        // shadowCode
        bgView.layer.shadowColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 0.9).cgColor
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bgView.layer.shadowOpacity = 1
        bgView.layer.shadowRadius = 10
        self.contentView.addSubview(bgView)
        
        let ww = (kScreenWidth - 28)/3
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: ww, height: 80)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: self.bgView.frame.size.width, height: 80), collectionViewLayout: layout)
        self.collectionView.register(MeItemCollectionCell.self , forCellWithReuseIdentifier: "MeItemCollectionCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.clear
        self.collectionView.isScrollEnabled = false
        self.bgView.addSubview(self.collectionView)
        
    
        
        self.addLayout()
    }
    
    func addLayout(){
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(82)
            make.bottom.equalTo(-5)
        }
        bgLayer1.frame = self.bgView.bounds
   
        
        self.collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(kScreenWidth - 28)
            make.height.equalTo(82)
        }
 
    }
    
  
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension MyEntranceTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource {
 
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MeItemCollectionCell", for: indexPath) as! MeItemCollectionCell
        switch indexPath.row {
        case 0:
            cell.titleLab.text = MMLocalizedString("my_appointment", comment: "我的预约")
            cell.imgView.image = UIImage.init(named: "icon_my_appointment")
        case 1:
            cell.titleLab.text = MMLocalizedString("my_favorite", comment: "我的收藏")
            cell.imgView.image = UIImage.init(named: "icon_used_car_collect")
        case 2:
            cell.titleLab.text = MMLocalizedString("order_history", comment: "订单记录")
            cell.imgView.image = UIImage.init(named: "icon_order_history")
        default:
            cell.titleLab.text = ""
            cell.imgView.image = UIImage.init()
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0://我的预约
            let vc = MyReservationViewController()
            self.viewController().navigationController?.pushViewController(vc, animated: true)
        case 1:
            let vc = MyFavoryViewController()
            self.viewController().navigationController?.pushViewController(vc, animated: true)
        default://订单记录
            let vc = OrderRecordViewController()
            self.viewController().navigationController?.pushViewController(vc, animated: true)
        }
    }
 
}
    
   
class MeItemCollectionCell: UICollectionViewCell {
    var imgView: UIImageView!
    var titleLab: UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.imgView = UIImageView.init(frame: .zero)
        self.imgView.contentMode = .scaleAspectFill
        self.contentView.addSubview(self.imgView)
        
        self.titleLab = UILabel.init(frame: CGRect.init(x: 0, y: self.imgView.bottom, width: self.width, height: 34))
        self.titleLab.backgroundColor = UIColor.clear
        self.titleLab.textAlignment = .center
        self.titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.numberOfLines = 0
        self.titleLab.adjustsFontSizeToFitWidth = true
        self.contentView.addSubview(self.titleLab)
       
        self.imgView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.height.width.equalTo(20)
        }
        
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(self.imgView.snp.bottom).offset(10)
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.height.equalTo(20)
        }
     
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
